package hanhns.test.listview;


import hanhns.test.R;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RowAdapter extends ArrayAdapter<Row> {

	private ArrayList<Row> items;
	private Context context;
	
	public RowAdapter(Context context, int textViewResourceId,
			List<Row> objects) {
		super(context, textViewResourceId, objects);
		this.items = (ArrayList<Row>) objects;
		this.context = context;
		// TODO Auto-generated constructor stub
	}
	
	static class ViewHolder{
		
		TextView title;
		TextView content;
		TextView id;
		ImageView icon;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ViewHolder holder;
        if (v == null) {
        	Log.e("Row adapter", "create new rows");
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(hanhns.test.R.layout.note_row, null);
            holder = new ViewHolder();
            holder.title = (TextView) v.findViewById(R.id.titleNote);
            holder.content = (TextView) v.findViewById(R.id.contentNote);
            holder.id = (TextView) v.findViewById(R.id.idNote);
            holder.icon = (ImageView) v.findViewById(R.id.icon);
            v.setTag(holder);
        } else {
        	Log.e("Row adapter", "dung  lai row bi an di");
        	holder = (ViewHolder) v.getTag();
        }
        Row o = items.get(position);
        if (o != null) {
        		holder.title.setText(o.getNoteTitle());
        		holder.content.setText(o.getNoteContent());
        		holder.id.setText(o.get_id() + "");
        		if(position % 2 == 0){
                	holder.icon.setImageResource(R.drawable.ic_tab_artists_white);
                }else{
                	holder.icon.setImageResource(R.drawable.icon) ;
                }
//                TextView tt = (TextView) v.findViewById(R.id.titleNote);
//                TextView bt = (TextView) v.findViewById(R.id.contentNote);
//                TextView id_note = (TextView) v.findViewById(R.id.idNote);
//                ImageView icon = (ImageView) v.findViewById(R.id.icon);
//               
//                
//                
//                if (tt != null) {
//                      tt.setText(o.getNoteTitle());                            }
//                if(bt != null){
//                      bt.setText(o.getNoteContent());
//                }
//                if(id_note != null){
//                	id_note.setText(o.get_id() + "");
//                }
//                if(position % 2 == 0){
//                	icon.setImageResource(R.drawable.ic_tab_artists_white);
//                }else{
//                	icon.setImageResource(R.drawable.icon) ;
//                }
        }
        Log.e("Rowadapter", "make view for row: " + position);
        return v;
	}
	
}
